package com.jht.bletool2java.activity.splash;


import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.config.BLEConfiguration;


public class SplashPresenter implements ISplash.Presenter {
    private static final String TAG = "SplashPresenter";

    private ISplash.UI splashActivity;


    public SplashPresenter(ISplash.UI splashActivity) {
        this.splashActivity = splashActivity;
    }

    /**
     * 是否是第一次启动，是：展示版本更新信息；否：跳转到HomeActivity；
     * 具体实现步骤：
     *  1.判断保存App配置信息的SharedPreference是否存在，如果不存在展示版本更新信息，存在则跳转到HomeActivity，并且全局保存配置信息；
     *
     */
    @Override
    public void startSplash() {
        if(DEBUG){
            Log.d(TAG, "startSplash: ");
        }
        SharedPreferences app_config = BLEApplication.getBLEApplicationContext().getSharedPreferences("app_config", Context.MODE_PRIVATE);
        boolean contains = app_config.contains(BLEConfiguration.KEY_APP_VERSION);
        SharedPreferences.Editor edit = app_config.edit();
        if(!contains){
            edit.putInt(BLEConfiguration.KEY_SCAN_TIME,20);
            edit.putString(BLEConfiguration.KEY_APP_VERSION,BLEApplication.getBLEConfiguration().getVersion());
            edit.apply();
            splashActivity.showVersionUpdateInformation();
        }else {
            int scanTime = app_config.getInt(BLEConfiguration.KEY_SCAN_TIME, 20);
            BLEApplication.getBLEConfiguration().setScanTime(scanTime);
            splashActivity.startHomeActivity();
        }

    }


}
